package com.example.testapplication;

import android.content.Context;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class listViewAdapter extends BaseAdapter {
    private List<ResolveInfo> infoList;
    private Context context;
    private LayoutInflater layoutInflater;

    listViewAdapter(List<ResolveInfo> infoList,Context context){
        this.infoList=infoList;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(this.context);
    }


    @Override
    public int getCount() {
        return infoList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view ==null){
            view = layoutInflater.inflate(R.layout.item_app,viewGroup,false);
            viewHolder = new ViewHolder();
            viewHolder.icon = view.findViewById(R.id.icon_iv);
            viewHolder.name = view.findViewById(R.id.name_tv);
            viewGroup.setTag(viewGroup);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.name.setText(infoList.get(i).activityInfo.loadLabel(context.getPackageManager()));
        viewHolder.icon.setImageDrawable(infoList.get(i).activityInfo.loadIcon(context.getPackageManager()));

        return view;
    }
    class ViewHolder{
        ImageView icon;
        TextView name;
    }
}
